/**
 * 验收
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Card, Col, DatePicker, Drawer, Form, Input, Row, Select, Statistic, Table} from 'antd';
import styles from '../../../index.less';
import ViewTitle from '../../../../components/ViewTitle';
import FormBase from '../../../../components/Base/FormBase';
import notify from "../../../../components/NotificationExtra";
import {FormField, SearchForm} from 'antd-extra';
import moment from 'moment';
import 'moment/locale/zh-cn';
import ModalProcureOrderDetail from "../../../procure/modal/ModalProcureOrderDetail";

moment.locale('zh-cn');
const {RangePicker} = DatePicker;
const _ = require('lodash');

const table = 'ProcureOrder';
const title = '采购单';
const {Option} = Select;
@Form.create()
@connect(({
            user,
            global,
            dynamicLabel,
            procure,
            store
          }) => ({...user, ...global, ...procure, ...dynamicLabel, ...store}))
export default class PaymentDataFaceAcceptance extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const {dispatch,materialType} = this.props;
    const payload = this.props.form.getFieldsValue();
    _.map(payload, (value, key) => {
      if (value === '') {
        payload[key] = undefined;
      }
    });

    dispatch && dispatch({
      type: 'global/getModel',
      payload: {table}
    });

    // dispatch && dispatch({
    //   type: 'vendor/findMany',
    //   payload: {}
    // });

    dispatch && dispatch({
      type: 'procure/findStatistic',
      payload: {materialType}
    });
    dispatch && dispatch({
      type: 'procure/findMany',
      payload: {materialType}
    });
  }

  getSearchFields = () => {
    const arr = [
      <FormField
        label="编码"
        name="code"
        key={1}
      >
        <Input allowClear placeholder={`请输入${title}编码`}/>
      </FormField>,
      <FormField
        label="供应商"
        name="vendor"
        key={2}
      >
        <Input allowClear placeholder="请输入供应商"/>
      </FormField>,

      <FormField
        label="创建时间"
        name="createdTs"
        key={3}
        initialValue={[this.getSearchStart1Months(), this.getDefaultSearchEndDate()]}
      >
        <RangePicker allowClear={true}
          // style={{ width: '150%' }}
        />
      </FormField>,
      <FormField
        label="审核状态"
        name="reviewStatus"
        key={4}
      >
        <Select allowClear placeholder="请选择">
          <Option value={'待审核'}>{'待审核'}</Option>
          <Option value={'同意'}>{'同意'}</Option>
          <Option value={'驳回'}>{'驳回'}</Option>
        </Select>
      </FormField>,
      <FormField
        label="验收状态"
        name="acceptanceStatus"
        key={5}
      >
        <Select allowClear placeholder="请选择">
          <Option value={'待验收'}>{'待验收'}</Option>
          <Option value={'验收通过'}>{'验收通过'}</Option>
          <Option value={'驳回'}>{'驳回'}</Option>
        </Select>
      </FormField>
    ];
    return arr;

  };

  render() {

    const {statistic = {}, _notify, orders = [], form} = this.props;
    const {showDetail, record = {}, current = 1, loading, total = 0} = this.state;

    if (_notify) {
      notify(_notify);
    }
    const {amount = 0, count = 0} = statistic;
    const model = this.props['ProcureOrderModel'];
    const cols = this.getTableColumns(model);
    cols.push({
      title: '操作', dataIndex: 'operation', key: 'operation',
      width: 160, fixed: 'right',
      render: (value, record) => {
        return (
          <span>
            {<a style={{marginRight: '6px'}}
                onClick={() => this.setState({showDetail: true, record})}>详情</a>}
            </span>
        );
      }
    });
    return (
      <Form className={styles.content}>
        <ViewTitle
          title={`${title}列表`}
          level={1}
        ></ViewTitle>
        <SearchForm
          style={{marginLeft: '6px', marginRight: '6px'}}
          onSearch={() => {
            this.state.current = 1;
            this.componentDidMount();
          }}
          onReset={this.handleReset}
          cols={3}
          min={6}
          expandText="展开"
          form={form}
        >
          {this.getSearchFields()}
        </SearchForm>
        <div style={{background: '#ECECEC', padding: '30px'}}>
          <Row gutter={16}>
            <Col span={12}>
              <Card>
                <Statistic
                  title="采购物料总数量"
                  value={count}
                  precision={2}
                  valueStyle={{color: '#3f8600'}}
                  suffix="KG"
                />
              </Card>
            </Col>
            <Col span={12}>
              <Card>
                <Statistic
                  title="采购物料总金额"
                  value={amount}
                  precision={2}
                  valueStyle={{color: '#cf1322'}}
                  suffix="元"
                />
              </Card>
            </Col>
          </Row>
        </div>
        <Table
          className={styles.antTable}
          bordered={true}
          columns={cols}
          dataSource={orders}
          scroll={{x: true}}
          // pagination={true}
          pagination={{
            current: this.state.current,
            total: this.state.total,
            showSizeChanger: true,
            onChange: page => {
              this.state.current = page;
              this.componentDidMount();
            },
            onShowSizeChange: (current, pageSize) => {
              this.state.current = 1;
              this.state.pageSize = pageSize;
              this.componentDidMount();
            }
          }}
          //scroll={{ x: 1300 }}
        ></Table>

        {showDetail && <Drawer
          visible={showDetail}
          title={`${title}详情`}
          placement="left"
          width={'80%'}
          style={{padding: 0}}
          onClose={() => {
            this.setState({showDetail: false});
            this.componentDidMount();
          }}
        >
          <ModalProcureOrderDetail
            values={record}
          > </ModalProcureOrderDetail>
        </Drawer>}
      </Form>
    );
  }
}
